package com.xh.bussiness.practice.controller;

import com.xh.bussiness.practice.bean.request.SavePracticeRequest;
import com.xh.bussiness.practice.service.IPracticeService;
import com.xh.bussiness.practice.model.Practice;
import com.xh.bussiness.practice.bean.request.PracticeRequest;
import com.xh.bussiness.practice.service.IPracticeStudentResultService;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;

import com.xh.core.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 作业表 前端控制器
 * </p>
 *
 * @author Pitt
 * @since 2021-01-26
 */
@RestController
@RequestMapping("/admin/practice")
public class PracticeController extends BaseController {

    @Autowired
    private IPracticeService practiceService;
    @Autowired
    private IPracticeStudentResultService practiceStudentResultService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @RequestMapping("/queryPage")
    public AjaxResult queryPage(XhPage page, PracticeRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceService.queryPracticePage(page,bean));
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @RequestMapping("/query")
    public AjaxResult query(PracticeRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",practiceService.queryPractice(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @RequestMapping(value="/exportAll")
    public AjaxResult exportAll(PracticeRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=practiceService.exportAll(request,exportHeaderBeans,bean,"queryPracticePage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param practice 实体对象
     * @return
     */
    @RequestMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated Practice practice){
        if(practice.getId()==null){
            if(practice.getStatus() == 1){
                practice.setPublishTime(new Date());
            }
            practiceService.insert(practice);
        }else{
            practiceService.update(practice);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @RequestMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        practiceService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        practiceService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 发布作业
     * @param request
     * @return
     */
    @PostMapping("/publishPractice")
    public AjaxResult publishPractice(HttpServletRequest request,@RequestBody SavePracticeRequest bean) {
        bean.setUserId(request.getHeader("userId"));
        String msg = practiceService.publishPractice(bean);
        if (!StringUtils.isEmpty(msg)) {

            return new AjaxResult(AjaxResultConstant.Result.ERROR, msg);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "发布成功");
    }

    /**
     * 作业查询
     * @param request
     * @return
     */
    @RequestMapping("/queryPublishPractice")
    public AjaxResult queryPublishPractice(@RequestBody PracticeRequest request) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceService.queryPublishPracticePage(request));
    }


    @RequestMapping("/queryPracticeDetailById")
    public AjaxResult queryPracticeDetailById(String practiceId) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceService.queryPracticeDetailById(practiceId));
    }

    @RequestMapping("/queryPracticeByClassId")
    public AjaxResult queryPracticeByClassId(XhPage page, PracticeRequest bean) {
        String userId = UserUtils.getCurrentUserId();
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, practiceService.queryPracticeByClassId(page, bean));
    }

    /**
     * 小程序 作业分析
     * @author 赵文博
     * @date 2021/4/22 14:45
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/practiceAnalysis")
    public AjaxResult practiceAnalysis(@RequestBody PracticeRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,practiceService.practiceAnalysis(bean));
    }

    /**
     * 逻辑删除
     * @author 赵文博
     * @date 2021/4/25 10:23
     * @param id
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/delete")
    public AjaxResult delete(String id){
        return practiceService.deletePracticeById(id)
                ? new AjaxResult(AjaxResultConstant.Result.SUCCESS) : new AjaxResult(AjaxResultConstant.Result.ERROR);
    }


    /**
     * 获取作业试题详情
     * @author 赵文博
     * @date 2021/4/25 13:57
     * @param practiceId
     * @param type
     * @return com.xh.core.bean.AjaxResult
     **/
    @GetMapping("/queryQuestionDetail")
    public AjaxResult queryQuestionByPracticeId(String practiceId,String type){
       List result =  practiceService.queryQuestionByPracticeId(practiceId,type);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,result);
    }

    /**
     * 试题作答情况分析
     * @author 赵文博
     * @date 2021/4/25 14:32
     * @param practiceId
     * @param questionId
     * @return com.xh.core.bean.AjaxResult
     **/
    @GetMapping("/questionAnalysis")
    public AjaxResult queryQuestionAnalysis(String practiceId, String questionId){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,
                practiceStudentResultService.queryQuestionAnalysis(practiceId,questionId));
    }


    /**
     * 普通作业作答情况分析
     * @author 赵文博
     * @date 2021/5/18 10:52
     * @param practiceId
     * @return com.xh.core.bean.AjaxResult
     **/
    @GetMapping("/queryPracticeAnalysis")
    public AjaxResult queryPracticeAnalysis(String practiceId){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,
                practiceStudentResultService.queryPracticeAnalysis(practiceId));
    }
}

